<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link href="/static/index/layui/css/layui.css" rel="stylesheet">
    <script src="/static/index/js/jquery-3.4.1.min.js"></script>
</head>

<body>
    <form class="layui-form">
        <div class="layui-form-item">
            <label class="layui-form-label">姓名</label>
            <div class="layui-input-block">
                <input type="text" name="name" required lay-verify="required" placeholder="请输入姓名" autocomplete="off"
                    class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">年龄</label>
            <div class="layui-input-block">
                <input type="number" name="age" required lay-verify="required" placeholder="请输入年龄" autocomplete="off" onkeypress='return(/[\d]/.test(String.fromCharCode(event.keyCode)))'
                    class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <div class="layui-btn" lay-submit id="submit" lay-filter="formDemo">立即提交</div>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>

    <script>
        $("#submit").click(function () {

            // 获取数据
            var name = $("input[name='name']").val();
            var age = $("input[name='age']").val();
            //拦截
            console.log(name)
            var url = "user";
            var data = {
                'name': name,
                'age': age,
            };
            // 提交
            $.post(url, data, function (res) {
                if(res.code==1){
                    alert(res.msg);
                }
            }, 'json');
        });
    </script>
</body>

</html>